package com.hs.system.controller;

import com.hs.common.core.domain.Result;
import com.hs.common.core.utils.StringUtils;
import com.hs.common.core.web.controller.BaseController;
import com.hs.common.core.web.domain.AjaxResult;
import com.hs.common.core.web.page.TableDataInfo;
import com.hs.common.security.annotation.InnerAuth;
import com.hs.common.security.annotation.RequiresLogin;
import com.hs.common.security.utils.SecurityUtils;
import com.hs.system.api.entity.User;
import com.hs.system.api.model.LoginUser;
import com.hs.system.service.MenuService;
import com.hs.system.service.RoleService;
import com.hs.system.service.UserRoleService;
import com.hs.system.service.UserService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * 用户信息
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private RoleService roleService;

    @Resource
    private MenuService menuService;


    /**
     * 获取用户列表
     */
    @RequiresLogin
    @GetMapping("/list")
    public TableDataInfo list() {
        List<User> list = userService.selectUserList();
        return getDataTable(list);
    }

    /**
     * 获取当前用户信息
     */
//    @RequiresLogin
    @InnerAuth
    @GetMapping("/info/{username}")
    public Result<LoginUser> info(@PathVariable("username") String username) {
        User user = userService.selectUserByUserName(username);
        if (StringUtils.isNull(user)) {
            return Result.fail("用户名或密码错误");
        }
        LoginUser userVo = new LoginUser();
        userVo.setUser(user);
        return Result.ok(userVo);
    }

    /**
     * 获取用户信息
     * @return 用户信息
     */
    @GetMapping("/info")
    public AjaxResult getInfo() {
        System.out.println("info 函数");
        User user = userService.selectUserById(SecurityUtils.getUserId());
        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(user);
//        // 权限集合
//        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
//        ajax.put("roles", roles);
//        ajax.put("permissions", permissions);
        return ajax;
    }


    /**
     * 注册用户信息
     */
    @InnerAuth
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody User user) {
        System.out.println("UserService中的注册方法");
        String username = user.getEmail();
        if (!userService.checkUserNameUnique(user)) {
            return Result.fail("保存用户'" + username + "'失败，该邮箱已注册");
        }

        //插入用户表
        userService.registerUser(user);
        //插入用户角色表

        System.out.println("user.getId="+user.getId());
        //默认给新注册的用户普通用户权限
        userRoleService.insertOne(user.getId(),2);
        return Result.ok();
    }

    /**
     * 修改用户
     */
    @RequiresLogin
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody User user) {
//        if (!userService.checkUserNameUnique(user)) {
//            return error("修改用户'" + user.getEmail() + "'失败，登录账号已存在");
//        } else if (StringUtils.isNotEmpty(user.getPhoneNumber()) && !userService.checkPhoneUnique(user)) {
//            return error("修改用户'" + user.getUserName() + "'失败，手机号码已存在");
//        } else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user)) {
//            return error("修改用户'" + user.getUserName() + "'失败，邮箱账号已存在");
//        }
//        system.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户
     */
    @RequiresLogin
//    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        if (ArrayUtils.contains(userIds, SecurityUtils.getUserId())) {
            return error("当前用户不能删除");
        }
        return toAjax(userService.deleteUserByIds(userIds));
    }
}
